from symbide.src.parsing import PATTERN
from parser import Parser

class NumberParser(Parser):
    '''
    This parser encapsulates all Numbers with sympy.Number(%s)
    '''
    
    #everything inside the following tags is ignored
    tags = (('<string>','</string>'),
            ('<ind>','</ind>'),
            ('<parser off>', '<parser on>'))
    
    def parse(self, text, **args):
        '''
        Encapsulate all numbers with Number(%)
        By default both integers and reals are regarded. 
        
        This can be changed with additional arguments:
            -integerNumbers
            -realNumbers
        '''
        
        self.flags = {'integerNumber':True,
                      'realNumber':True}
        
        self.flags.update(**args)
    
        #now comes the parsing
        self._updateTagsList(text)
        def subst(number):
            if self._insideTag(number.start()):
                return number.group()
            return 'Number(%s)'%number.group()
        if self.flags['realNumber']:
            text = PATTERN['real'].sub(subst, text)
        if self.flags['integerNumber']:
            text = PATTERN['integer'].sub(subst, text)
        return text
    
    